<template>
  <div :class="className" :style="{ height: height }" class="category" />
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '370px'
    },
    option: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart(this.option)
    window.addEventListener('resize', this.resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.resizeHanlder)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getChart() {
      return this.chart
    },
    initChart(num) {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(num)
    },
    resizeHanlder() {
      if (this.chart) {
        this.chart.resize()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.category {
  background: #ffffff;
  border-radius: 8px;
}
</style>
